package com.administrator.scoreManageSys.controller.system;

import com.administrator.scoreManageSys.service.SystemUserAuthenticationService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import static com.administrator.scoreManageSys.system.SystemConstant.CURRENT_SYSTEM_USER;
import static com.administrator.scoreManageSys.system.SystemConstant.CURRENT_SYSTEM_USER_ROLE;


/**
 * 系统用户认证控制器
 *
 * @author Administrator
 */
@RestController
@RequestMapping(value = "/system/authentication")
public class SystemUserAuthenticationController {

    @Resource
    private SystemUserAuthenticationService systemUserAuthenticationService;

    /** 登录接口
     * @param data 登录数据
     * @return 返回token
     */
    @PostMapping(value = "/login")
    public JSONObject systemUserLogin(@RequestBody JSONObject data) {
        try {
            //获取账号
            String account = data.getString("account");
            //获取密码
            String password = data.getString("password");
            return systemUserAuthenticationService.login(account, password);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("登录 异常");
        }
    }
    @PostMapping(value = "/forgotPassword")
    public JSONObject forgotPassword(@RequestBody JSONObject data) {
        try {
            //获取账号
            String account = data.getString("account");
            //密保问题
            Long securityAnswer = data.getLong("securityAnswer");
            //密保答案
            String securityQuestion = data.getString("securityQuestion");
            //新密码
            String newPassword = data.getString("newPassword");
            return systemUserAuthenticationService.forgotPassword(account, securityAnswer,securityQuestion,newPassword);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("忘记密码 异常");
        }
    }

    /** 登出接口
     * @param request 用户数据
     * @return 登出结果
     */
    @PostMapping(value = "/signOut")
    public JSONObject systemUserSignOut(HttpServletRequest request) {
        try {
            Long userId = Long.valueOf(request.getAttribute(CURRENT_SYSTEM_USER).toString());
            return systemUserAuthenticationService.systemUserSignOut(userId);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("登出 异常");
        }
    }

    /** 获取菜单
     * @param request 用户数据
     * @return 菜单数据
     */
    @PostMapping(value = "/getUserMenu")
    public JSONObject getUserMenu(HttpServletRequest request) {
        try {
            Long role = Long.valueOf(request.getAttribute(CURRENT_SYSTEM_USER_ROLE).toString());
            return systemUserAuthenticationService.getUserMenu(role);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("获取菜单 异常");
        }
    }

    /** 获取登录用户所有信息
     * @param request 用户数据
     * @return 菜单数据
     */
    @PostMapping(value = "/getUser")
    public JSONObject getUser(HttpServletRequest request) {
        try {
            Long userId = Long.valueOf(request.getAttribute(CURRENT_SYSTEM_USER).toString());
            return systemUserAuthenticationService.getUser(userId);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("获取登录用户所有信息 异常");
        }
    }


    @PostMapping(value = "/changePassword")
    public JSONObject changePassword(@RequestBody JSONObject data,HttpServletRequest request) {
        try {
            Long userId = Long.valueOf(request.getAttribute(CURRENT_SYSTEM_USER).toString());
            String oldPassword = data.getString("oldPassword");
            String newPassword = data.getString("newPassword");
            return systemUserAuthenticationService.changePassword(userId,oldPassword,newPassword);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("修改密码 异常");
        }
    }

    /** 设置密保
     * @param request 用户数据
     * @return 菜单数据
     */
    @PostMapping(value = "/setPasswordProtection")
    public JSONObject setPasswordProtection(@RequestBody JSONObject data,HttpServletRequest request) {
        try {
            Long userId = Long.valueOf(request.getAttribute(CURRENT_SYSTEM_USER).toString());
            JSONArray passwordProtection = data.getJSONArray("passwordProtection");
            return systemUserAuthenticationService.setPasswordProtection(passwordProtection,userId);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("设置密保 异常");
        }
    }
    /** 更新密保
     * @param request 用户数据
     * @return 菜单数据
     */
    @PostMapping(value = "/renewPasswordProtection")
    public JSONObject renewPasswordProtection(@RequestBody JSONObject data,HttpServletRequest request) {
        try {
            Long userId = Long.valueOf(request.getAttribute(CURRENT_SYSTEM_USER).toString());
            JSONArray passwordProtection = data.getJSONArray("passwordProtection");
            return systemUserAuthenticationService.renewPasswordProtection(passwordProtection,userId);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("更新密保 异常");
        }
    }
}
